From a5c868a9dc2eaeba66981e9bc7a99f1693d4a80e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 1 Nov 2014 11:51:58 -0700 Subject: [PATCH] Really fix `cargo test` and fix an OUT_DIR bug Assorted bug fixes discovered while migrating packages to using this build command infrastructure. --- src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/ops/cargo_rustc/custom_build.rs | 7 ++- src/cargo/ops/cargo_rustc/mod.rs | 2 +- tests/test_cargo_compile_custom_build.rs | 75 +++++++++++++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 8b25881fe..6f47fe380 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -129,7 +129,7 @@ pub fn compile_pkg(package: &Package, options: &mut CompileOptions) }; let targets = to_build.get_targets().iter().filter(|target| { - match env { + target.get_profile().is_custom_build() || match env { // doc-all == document everything, so look for doc targets "doc" | "doc-all" => target.get_profile().get_env() == "doc", env => target.get_profile().get_env() == env, diff --git a/src/cargo/ops/cargo_rustc/custom_build.rs b/src/cargo/ops/cargo_rustc/custom_build.rs index b46505f12..727aebaec 100644 --- a/src/cargo/ops/cargo_rustc/custom_build.rs +++ b/src/cargo/ops/cargo_rustc/custom_build.rs @@ -81,7 +81,8 @@ pub fn prepare(pkg: &Package, target: &Target, cx: &mut Context) let pkg_name = pkg.to_string(); let native_libs = cx.native_libs.clone(); let all = (lib_name.clone(), pkg_name.clone(), native_libs.clone(), - script_output.clone()); + script_output.clone(), old_build_output.clone(), + build_output.clone()); try!(fs::mkdir(&script_output, USER_RWX)); @@ -165,9 +166,11 @@ pub fn prepare(pkg: &Package, target: &Target, cx: &mut Context) try!(fingerprint::prepare_build_cmd(cx, pkg, Some(target))); let dirty = proc(tx: Sender) { try!(work(tx.clone())); dirty(tx) }; let fresh = proc(tx) { - let (lib_name, pkg_name, native_libs, script_output) = all; + let (lib_name, pkg_name, native_libs, script_output, + old_build_output, build_output) = all; let new_loc = script_output.join("output"); try!(fs::rename(&old_script_output.join("output"), &new_loc)); + try!(fs::rename(&old_build_output, &build_output)); let mut f = try!(File::open(&new_loc).map_err(|e| { human(format!("failed to read cached build command output: {}", e)) })); diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index dc9798e5c..a3a9c83d5 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -175,7 +175,7 @@ fn compile<'a, 'b>(targets: &[&'a Target], pkg: &'a Package, // skipped entirely match pkg.get_manifest().get_links() { Some(lib) => { - if cx.native_libs.lock().contains_key_equiv(&lib) { + if cx.native_libs.lock().contains_key_equiv(lib) { continue } } diff --git a/tests/test_cargo_compile_custom_build.rs b/tests/test_cargo_compile_custom_build.rs index 872d87116..52bd0b2e0 100644 --- a/tests/test_cargo_compile_custom_build.rs +++ b/tests/test_cargo_compile_custom_build.rs @@ -450,6 +450,8 @@ test!(testing_and_such { execs().with_status(0) .with_stdout(format!("\ {compiling} foo v0.5.0 (file://[..]) +{running} `[..]build-script-build` +{running} `rustc [..] --crate-name foo [..]` {running} `rustc [..] --test [..]` {running} `[..]foo-[..]` @@ -677,3 +679,76 @@ test!(build_cmd_with_a_build_cmd { -L [..]target -L [..]target[..]deps` ", compiling = COMPILING, running = RUNNING).as_slice())); }) + +test!(out_dir_is_preserved { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.5.0" + authors = [] + build = "build.rs" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + use std::os; + use std::io::File; + fn main() { + let out = os::getenv("OUT_DIR").unwrap(); + File::create(&Path::new(out).join("foo")).unwrap(); + } + "#); + + // Make the file + assert_that(p.cargo_process("build").arg("-v"), + execs().with_status(0)); + p.root().move_into_the_past().unwrap(); + + // Change to asserting that it's there + File::create(&p.root().join("build.rs")).write_str(r#" + use std::os; + use std::io::File; + fn main() { + let out = os::getenv("OUT_DIR").unwrap(); + File::open(&Path::new(out).join("foo")).unwrap(); + } + "#).unwrap(); + p.root().move_into_the_past().unwrap(); + assert_that(p.process(cargo_dir().join("cargo")).arg("build").arg("-v"), + execs().with_status(0)); + + // Run a fresh build where file should be preserved + assert_that(p.process(cargo_dir().join("cargo")).arg("build").arg("-v"), + execs().with_status(0)); + + // One last time to make sure it's still there. + File::create(&p.root().join("foo")).unwrap(); + assert_that(p.process(cargo_dir().join("cargo")).arg("build").arg("-v"), + execs().with_status(0)); +}) + +test!(output_separate_lines { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.5.0" + authors = [] + build = "build.rs" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { + println!("cargo:rustc-flags=-L foo"); + println!("cargo:rustc-flags=-l foo"); + } + "#); + assert_that(p.cargo_process("build").arg("-v"), + execs().with_status(0) + .with_stdout(format!("\ +{compiling} foo v0.5.0 (file://[..]) +{running} `rustc build.rs [..]` +{running} `[..]foo-[..]build-script-build` +{running} `rustc [..] --crate-name foo [..] -L foo -l foo` +", compiling = COMPILING, running = RUNNING).as_slice())); +}) -- 2.30.2